/*
 * Copyright ©2015-2021 Jaemon. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gitee.jaemon.spring.adorner.util;

import java.lang.annotation.Annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * 注解工具类
 *
 * @author Jaemon
 * @since 1.0
 */
public class AnnotationUtils {
    public final static List<Class<? extends Annotation>> excludeAnnotations = new ArrayList<>();

    static {
        excludeAnnotations.add(Target.class);
        excludeAnnotations.add(Retention.class);
        excludeAnnotations.add(Documented.class);
    }

    private AnnotationUtils() {
    }

    /**
     * 类是否被期望的注解修饰
     *
     * @param clazz
     *      类名
     * @param except
     *      期望的注解
     * @param <T>
     *      T {@link Annotation}
     * @return
     *      true | false
     */
    public static <T extends Annotation> boolean classAnnotation(Class clazz, Class<T> except) {
        for (Annotation annotation : clazz.getAnnotations()) {
            if (containAnnotation(new Annotation[]{annotation}, except)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 方法是否被期望的注解修饰
     *
     * @param method
     *      方法名
     * @param except
     *      期望的注解
     * @param <T>
     *      T {@link Annotation}
     * @return
     *      true | false
     */
    public static <T extends Annotation> boolean methodAnnotation(Method method, Class<T> except) {
        for (Annotation methodAnnotation : method.getAnnotations()) {
            if (containAnnotation(new Annotation[]{methodAnnotation}, except)) {
                return true;
            }
        }

        return false;
    }


    /**
     * 注解数组中是否包含期望的数组
     *
     * @param annotations
     *      注解数组
     * @param except
     *      期望的注解
     * @param <T>
     *      T {@link Annotation}
     * @return
     *      true | false
     */
    public static <T extends Annotation> boolean containAnnotation(Annotation[] annotations, Class<T> except) {
        T t = contains(annotations, except);
        if (t == null) {
            return false;
        }
        return true;
    }

    /**
     * 注解数组中是否包含期望的数组并返回该注解
     *
     * @param annotations
     *      注解数组
     * @param except
     *      期望的类
     * @param <T>
     *      T {@link Annotation}
     * @return
     *      被期望注解的修饰的注解 | null
     */
    public static <T extends Annotation> T contains(Annotation[] annotations, Class<T> except) {
        for (int i = 0, length = annotations.length; i < length; i++) {
            Annotation annotation = annotations[i];

            Class<? extends Annotation> annotationClass = annotation.annotationType();
            if (excludeAnnotations.contains(annotationClass)) {
                continue;
            }
            if (annotationClass == except) {
                return (T) annotation;
            }

            if (annotationClass.getAnnotations().length > 0) {
                T contains = contains(annotationClass.getAnnotations(), except);
                if (contains != null) {
                    return contains;
                } else if (contains == null && i == length - 1) {
                    return null;
                }
                continue;
            }
        }

        return null;
    }

}